package hlhj.fhp.supreme.activitys

import android.annotation.SuppressLint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.AppointPop
import hlhj.fhp.supreme.customview.CityPop
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.activity_appointment_aty.*
import java.util.ArrayList

class AppointmentAty : BaseAty(), CityPop.OnResult, AppointPop.OnPopClic {
    private var cid=""
    private var pid=""
    private var aid=""
    private var type=""
    private lateinit var citiPop:CityPop
    private lateinit var appointPop:AppointPop
    override fun getContentId(): Int {
        return R.layout.activity_appointment_aty
    }

    override fun initView() {
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","appoint_type")
                .execute(object :JsonCallBack<PhoneBean>(this){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response!!.body()
                        appointPop= AppointPop(this@AppointmentAty, this@AppointmentAty,bean.data.appoint_type as ArrayList<String>)

                    }
                })
        citiPop=CityPop(this,this)
    }
    @SuppressLint("RtlHardcoded")
    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }

        btChooseType.setOnClickListener {
            if (appointPop.isShowing){
                appointPop.dismiss()
            }else{
                appointPop.showAtLocation(dvdv,Gravity.RIGHT,0,0)
//                appointPop.showAsDropDown(btChooseType,0,0)
            }
        }
        btChooseCity.setOnClickListener {
            if (citiPop.isShowing){
                citiPop.dismiss()
            }else{
                citiPop.showAtLocation(father,Gravity.BOTTOM,0,0)
            }
        }

        btCommit.setOnClickListener {
            if (etName.text.isEmpty()||etPhone.text.isEmpty()||et.text.isEmpty()||etType.text.isEmpty()){
                if (etName.text.isEmpty()){
                    MyUtils.toast("姓名未填写")
                    return@setOnClickListener
                }
                if (etPhone.text.isEmpty()){
                    MyUtils.toast("手机号未填写")
                    return@setOnClickListener
                }
                if (et.text.isEmpty()){
                    MyUtils.toast("所在地未填写")
                    return@setOnClickListener
                }
                if (etType.text.isEmpty()){
                    MyUtils.toast("类型未填写")
                    return@setOnClickListener
                }


            }else{
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.APPOINTMENT)
                        .params("mobile",etPhone.text.toString())
                        .params("type",this.type)
                        .params("name",etName.text.toString())
                        .params("province_id",this.cid)
                        .params("city_id",this.pid)
                        .params("area_id",this.cid)
                        .execute(object :JsonCallBack<BaseBean>(this){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast(bean?.msg)
                                    finish()
                                }else{
                                    MyUtils.toast(bean?.msg)
                                }
                            }
                        })
            }
        }
    }

    override fun onResult(cTv: String, pTv: String, aTv: String, cid: String, pid: String, aId: String) {
        this.cid=cid
        this.pid=pid
        this.aid=aId
        et.setText(cTv+pTv+aTv)

    }

    override fun onPopClockListener(type: String) {
        this.type=type
        etType.setText(type)

    }
}
